package com.tanhua.server.controller;

import com.tanhua.server.service.SoulService;
import com.tanhua.server.vo.Answers;
import com.tanhua.server.vo.QuestionListVo;
import com.tanhua.server.vo.TestResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("testSoul")
@RestController
@Slf4j
public class SoulController {
    @Autowired
    private SoulService soulService;

    /**
     * 问卷列表
     * @return
     */
    @GetMapping
    public List<QuestionListVo> SoulQuestionnaire() {
        List<QuestionListVo> questionListVoList = this.soulService.SQuestionnaireLike();
        for (QuestionListVo questionListVo : questionListVoList) {
            System.out.println(questionListVo);
        }
        return questionListVoList;
    }

    /**
     * 提交问卷
     *
     * @param answers
     */
    @PostMapping("testSoul")
    public void TestSoul(@RequestBody Map<String, List<Answers>> answers) {
        List<Answers> answersList = answers.get("answers");
        this.soulService.testSoul(answersList);
    }

    /**
     * 测试结果
     * @param id
     * @return
     */
    @GetMapping("testSoul/report/{id}")
    public ResponseEntity<TestResultVo> TestResult(@PathVariable("id") Long id) {
        try {
            TestResultVo testResultVo = this.soulService.testResult(id);
            return ResponseEntity.ok(testResultVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
